package com.wie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwb.util.Message;
import com.gwb.util.MoonshotAiUtils;
import com.gwb.util.RoleEnum;
import com.wie.constant.QianfanConstant;
import com.wie.domain.dto.ProvinceGdpDTO;
import com.wie.domain.entity.GdpData;
import com.wie.domain.entity.ProvinceGdp;
import com.wie.domain.vo.GdpDataVO;
import com.wie.domain.vo.ProvinceGdpVO;
import com.wie.service.ProvinceGdpService;
import com.wie.mapper.ProvinceGdpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
* @author WiedersehenM
* @description 针对表【province_gdp(中国各省gdp)】的数据库操作Service实现
* @createDate 2024-10-20 16:25:45
*/
@Service
public class ProvinceGdpServiceImpl extends ServiceImpl<ProvinceGdpMapper, ProvinceGdp>
    implements ProvinceGdpService{

    @Autowired
    private ProvinceGdpMapper provinceGdpMapper;

    /**
     * 获取某一年某省份的gdp
     * @param provinceGdpDTO
     * @return
     */
    public ProvinceGdpVO getProvinceGdpData(ProvinceGdpDTO provinceGdpDTO) {
        // 1. 在数据库中查询有没有需要的值
        String province = provinceGdpDTO.getProvince();
        String year = provinceGdpDTO.getYear();
        HashMap<String, Object> wrapperMap = new HashMap<>();
        wrapperMap.put("province", province);
        wrapperMap.put("year", year);
        List<ProvinceGdp> provinceGdpDB = provinceGdpMapper.selectByMap(wrapperMap);
        if(provinceGdpDB.size() != 0) {
            ProvinceGdp provinceGdp = provinceGdpDB.get(0);
            ProvinceGdpVO provinceGdpVO = ProvinceGdpVO.builder()
                    .province(province)
                    .year(year)
                    .gdp(provinceGdp.getGdp())
                    .build();
            return provinceGdpVO;
        }
        // 2. 调用ai接口查询
        // 3. 指定查询信息
        String message = province + year + "年gdp(亿)" + ", 只要数字, 请用“省或市的名称...年的gdp为...“这种格式回答";
        List<Message> messages = CollUtil.newArrayList(
                new Message(RoleEnum.system.name(), message)
        );
        MoonshotAiUtils moonshotAiUtils = new MoonshotAiUtils();
        String result = moonshotAiUtils.doChat("moonshot-v1-8k", messages);
        // 4. 截取有效信息
        int beginIndex = result.indexOf("为") + 1;
        int endIndex = result.indexOf("亿") + 1;
        String gdp = result.substring(beginIndex, endIndex);
        // 封装GdpData, 将数据存入数据库
        ProvinceGdp provinceGdp = new ProvinceGdp().builder()
                .province(province)
                .year(year)
                .gdp(gdp)
                .create_time(LocalDateTime.now())
                .build();
        provinceGdpMapper.insert(provinceGdp);
        // 封装GdpDataVO，并返回
        ProvinceGdpVO provinceGdpVO = ProvinceGdpVO.builder()
                .province(province)
                .year(year)
                .gdp(gdp)
                .build();
        return provinceGdpVO;
    }
}




